// pages/myOrder/myOrder.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myHttp: app.globalData.myHttp,
    orders: [],
    appointmentTime: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },


  //取消预约
  toCancelAppointment(event) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认取消订单？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.toCancelAppointment2(event)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    // this.onShow()
  },



  toCancelAppointment2(event) {
    var that = this
    var index = event.currentTarget.dataset.bindex
    var token = "Bearer "+wx.getStorageSync('token')
    wx.request({
      url: this.data.myHttp + '/order/orderCancel',
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Authorization': token
      },
      data: {
        orderid: that.data.orders[index].orderid,
      },
      success(res) {
        console.log(res.data)
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      },
      complete(){
        that.showPage()
      }
    })

  },


  //去付款
  toPay(event) {
    var index = event.currentTarget.dataset.bindex
    wx.navigateTo({
      url: '../orderdetails/orderdetails?order_ID=' + this.data.orders[index].id + '&hospitalId=' + this.data.orders[index].hospitalid,
    })
  },


  //去退号退款
  toRefund(event) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认取消订单？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.toRefund2(event)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toRefund2(event){
    var that = this
    var index = event.currentTarget.dataset.bindex
    var token = "Bearer "+wx.getStorageSync('token')
    wx.request({
      url: this.data.myHttp + '/order/reseCancel',
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Authorization': token
      },
      data: {
        orderid: that.data.orders[index].orderid,
        hospitalid: that.data.orders[index].hospitalid
      },
      success(res) {
        console.log(res.data)
        if(res.data.success){
          that.showPage()
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  //点击挂号订单显示我的挂号订单详情
  toMyOrderDetails(event) {
    var index = event.currentTarget.dataset.bindex
    console.log(index)
    wx.navigateTo({
      url: '../myOrderDetails/myOrderDetails?order_ID=' + event.currentTarget.dataset.order.id + '&hospitalId=' + event.currentTarget.dataset.order.hospitalid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  //获取页面数据
  showPage() {
    var that = this
    var currentUser = wx.getStorageSync("user")
    var token = "Bearer "+wx.getStorageSync('token')
    //加载我的订单中个订单的内容
    wx.request({
      url: this.data.myHttp + '/order/getOrderList',
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Authorization': token
      },
      data: {
        orderType: 2,
        userId: currentUser.userID,
        isPay: false,
        current: 0,
        size: 10
      },
      success(res) {
        var orders = res.data.obj.records
        var orderStatus = []
        var status = ''
        orders.forEach((item) => {
          item.outpdate = item.outpdate ? item.outpdate.substring(0,10) : '-'
          switch (item.status) {
            case -4:
              status = "等待退款"
              break;
            case -3:
              status = "等待退款"
              break;
            case -2:
              status = "已退款"
              break;
            case -1:
              status = "已取消"
              break;
            case 0:
              status = "订单未生成"
              break;
            case 1:
              status = "待支付"
              break;
            case 2:
              status = "支付成功"
              break;
            case 3:
              status = "退款成功"
              break;
            case 4:
              status = "线下支付取号"
              break;
          }
          orderStatus.push(status)
        });

        that.setData({
          orders: orders,
          orderStatus: orderStatus
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.showPage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    setTimeout(function() {
      //complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
    this.showPage()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})